package cn.gson.boot.controller;

import cn.gson.boot.common.MD5Util;
import cn.gson.boot.model.pojos.User;
import cn.gson.boot.model.pojos.UserLogin;
import cn.gson.boot.model.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/log")
//@SessionAttributes({"user"})
public class LoginController {

    @Autowired
    LoginService loginService;

    @RequestMapping("/login")
    @ResponseBody
    public String login(@RequestBody User user, HttpSession session) {

        System.out.println("as:"+user.getEmpName());
        System.out.println("MD5加密的as:"+user.getEmpPwd());
        System.out.println("==:"+MD5Util.convertMD5(MD5Util.convertMD5(user.getEmpPwd())));
       User uesrOk = loginService.logingPD(user.getEmpName(),user.getEmpPwd());
       System.out.println("判断是否登录成功："+uesrOk);
       String fh = "";
        if (uesrOk == null) {
            fh = "400";
        } else {
            session.removeAttribute("user");
            session.setAttribute("user", uesrOk);
            session.setMaxInactiveInterval(0);//setMaxInactiveInterval设置的是当前会话的失效时间，不是整个web的时间，单位为以秒计算。如果设置的值为零或负数，
            // 则表示会话将永远不会超时。常用于设置当前会话时间。
            fh = "200";
        }
        System.out.println("fh:"+fh);
        return fh;
    }

    @RequestMapping("loginOk")
    @ResponseBody
    public User loginOk(@RequestBody User user) {
        User uesrOk = loginService.logingPD(user.getEmpName(),user.getEmpPwd());
        return uesrOk;
    }


    //测试测试
    @RequestMapping("/ceshiCeShi")
    @ResponseBody
    public List<UserLogin> ceshiCeShi(String empName, HttpSession session){
        List<UserLogin> users = (List<UserLogin>) loginService.ceshiCeShi(empName);
        session.removeAttribute("users");
        session.setAttribute("users",users);
        session.setMaxInactiveInterval(0);//以秒为单位，即在没有活动120分钟后，session将失效
        return users;
    }
}
